package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.OrderReturnReason;
import com.itheima.mapper.OrderReturnReasonMapper;
import com.itheima.service.OmsOrderReturnReasonService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @author zhangpeng
 */
@DubboService
public class OmsOrderReturnReasonServiceImpl implements OmsOrderReturnReasonService {
    @Autowired
    OrderReturnReasonMapper omsOrderReturnReasonMapper;

    //分页查询退货原因
    @Override
    public PageBeanVo list(Integer pageNum, Integer pageSize) {
        //1.创建分页对象
        Page<OrderReturnReason> page = new Page<>(pageNum, pageSize);
        //2.创建条件对象
        LambdaQueryWrapper<OrderReturnReason> qw = new LambdaQueryWrapper<>();
        //3.追加排序条件,按照时间降序操作
        qw.orderByDesc(OrderReturnReason::getCreateTime);
        //4.调用mapper获取分页退货原因
        Page<OrderReturnReason> returnReasonPage = omsOrderReturnReasonMapper.selectPage(page, qw);
        //5.封装pageBeanVo
        PageBeanVo pageBeanVo = new PageBeanVo(pageNum, pageSize, returnReasonPage.getTotal(), returnReasonPage.getRecords());
        return pageBeanVo;
    }


    //获取单个退货原因详情信息
    @Override
    public OrderReturnReason findById(Integer id) {
        //调用mapper获取单个退货原因对象
        OrderReturnReason omsOrderReturnReason = omsOrderReturnReasonMapper.selectById(id);
        return omsOrderReturnReason;
    }

    //批量删除退货原因
    @Override
    public int delete(Integer[] ids) {
        int count = 0;
        //遍历数组
        for (Integer id : ids) {
            //调用mapper完程删除操作
            omsOrderReturnReasonMapper.deleteById(id);
            count++;
        }
        return count;
    }


    //添加退货原因
    @Override
    public int add(OrderReturnReason omsOrderReturnReason) {
        //调用mapper完程新增曹组哟
        //设置添加时间
        omsOrderReturnReason.setCreateTime(new Date(System.currentTimeMillis()));
        int count = omsOrderReturnReasonMapper.insert(omsOrderReturnReason);
        return count;
    }


    // //修改退货原因启用状态
    @Override
    public int updateStatus(Integer ids, Integer status) {
        //1.调用mapper获取单个退货原因对象
        OrderReturnReason omsOrderReturnReason = omsOrderReturnReasonMapper.selectById(ids);
        //2.更新对象中的状态
        omsOrderReturnReason.setStatus(status);
        //3.调用mapper完程更新操作
        int count = omsOrderReturnReasonMapper.updateById(omsOrderReturnReason);

        return count;
    }


    //修改退货原因
    @Override
    public int updateById(Integer id, OrderReturnReason omsOrderReturnReason) {
        //1.根据id获取退货详情对象
        OrderReturnReason returnReason = omsOrderReturnReasonMapper.selectById(id);
        //2.封装退货原因
        returnReason.setName(omsOrderReturnReason.getName());
        returnReason.setSort(omsOrderReturnReason.getSort());
        returnReason.setStatus(omsOrderReturnReason.getStatus());


        //3.调用mapper完成修改操作
        int count = omsOrderReturnReasonMapper.updateById(returnReason);
        return count;
    }
}
